package com.example.bookkeep.login;

import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bookkeep.R;
import com.example.bookkeep.Utils.URLUtils;
import com.example.bookkeep.Utils.OkHttpUtil;

import org.json.JSONException;
import org.json.JSONObject;
import okhttp3.*;

public class VerifyEmailActivity extends AppCompatActivity {

    private EditText etVerifyUsername, etVerifyCode;
    private String receivedCode = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_email);

        etVerifyUsername = findViewById(R.id.etVerifyUsername);
        etVerifyCode = findViewById(R.id.etVerifyCode);
        Button btnSendCode = findViewById(R.id.btnSendCode);
        Button btnVerify = findViewById(R.id.btnVerify);

        btnSendCode.setOnClickListener(v -> {
            String username = etVerifyUsername.getText().toString().trim();
            if (username.isEmpty()) {
                Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                return;
            }

            new Thread(() -> {
                try {
                    JSONObject json = new JSONObject();
                    json.put("username", username);

                    RequestBody body = RequestBody.create(json.toString(), MediaType.parse("application/json"));
                    Request request = new Request.Builder()
                            .url(URLUtils.BASE_URL + "/emailCode")
                            .post(body)
                            .build();

                    Response response = OkHttpUtil.getInstance().newCall(request).execute();
                    if (response.isSuccessful()) {
                        JSONObject result = new JSONObject(response.body().string());
                        if (result.getInt("code") == 1) {
                            receivedCode = result.getString("content");
                            runOnUiThread(() -> Toast.makeText(this, "验证码已发送", Toast.LENGTH_SHORT).show());
                        } else {
                            runOnUiThread(() -> {
                                try {
                                    Toast.makeText(this, "发送失败：" + result.getString("message"), Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {
                                    throw new RuntimeException(e);
                                }
                            });
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        });

        btnVerify.setOnClickListener(v -> {
            String inputCode = etVerifyCode.getText().toString().trim();
            String username = etVerifyUsername.getText().toString().trim();
            if (inputCode.equals(receivedCode)) {
                Intent intent = new Intent(VerifyEmailActivity.this, ForgotPasswordActivity.class);
                intent.putExtra("username", username);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(this, "验证码不正确", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
